package com.gy.smartmanagement.process.processdevice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gy.smartmanagement.R;
import com.gy.smartmanagement.process.processdevice.mvp.FarmBean;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class ProfileFarmAdapter extends RecyclerView.Adapter<ProfileFarmAdapter.ViewHolder>{
  private Context context;
  private List<FarmBean> addressBeanList;
  private OnItemClickListener listener;

  public ProfileFarmAdapter(List<String> dataList, Context context) {
    this.context = context;
    addressBeanList = new ArrayList<>();
    for (String address : dataList) {
      FarmBean bean = new FarmBean();
      bean.setFarm(address);
      addressBeanList.add(bean);
    }
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.item_farm, parent, false);
    return new ViewHolder(view);
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    final FarmBean bean = addressBeanList.get(position);
    holder.address.setText(bean.getFarm());
    if (bean.isSelected()) {
      holder.address.setTextColor(context.getResources().getColor(R.color.white));
      holder.address.setBackground(context.getResources().getDrawable(R.drawable.bg_blue_corner5));
    } else {
      holder.address.setTextColor(context.getResources().getColor(R.color.black));
      holder.address.setBackground(context.getResources().getDrawable(R.drawable.bg_white_corner5));
    }
    holder.address.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        for (FarmBean addressBean : addressBeanList) {
          addressBean.setSelected(false);
        }
        bean.setSelected(true);
        notifyDataSetChanged();
        if (listener != null) {
          listener.onClick(bean.getFarm());
        }
      }
    });
  }

  @Override
  public int getItemCount() {
    if (addressBeanList == null) {
      return 0;
    } else
      return addressBeanList.size();
  }

  class ViewHolder extends RecyclerView.ViewHolder {
    private TextView address;

    public ViewHolder(View itemView) {
      super(itemView);
      address = itemView.findViewById(R.id.buffet_address_tv);
    }
  }

  public void setItemOnClickListener(OnItemClickListener listener) {
    this.listener = listener;
  }

  public interface OnItemClickListener {
    void onClick(String address);
  }
}
